package org.csu.backofficemanagersys.controller;

import com.alibaba.fastjson.JSONObject;
import org.csu.backofficemanagersys.Response.ResponseMsg;
import org.csu.backofficemanagersys.domain.AccountLog;
import org.csu.backofficemanagersys.domain.AdminLog;
import org.csu.backofficemanagersys.service.AccountLogService;
import org.csu.backofficemanagersys.service.AdminLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mypetstore/log")
public class LogController {
    @Autowired
    AdminLogService adminLogService;
    @Autowired
    AccountLogService accountLogService;

    @GetMapping("/admin/index")
    public ResponseMsg<List<AdminLog>> getAdminLog(){
        return ResponseMsg.createForSuccess(adminLogService.getAllAdminLog());
    }
    @GetMapping("/admin/table")
    public ResponseMsg<JSONObject> TableP(int currentPage, int size){
        try {
            List<AdminLog> adminLogList = adminLogService.getAllAdminLog();
            JSONObject jsonObject = new JSONObject();
            List<AdminLog> adminLogs;
            if(adminLogList.size()<size*currentPage && adminLogList.size()>(currentPage-1)*size){
                adminLogs = adminLogList.subList(size * (currentPage - 1), adminLogList.size());
            }else {
                adminLogs = adminLogList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",adminLogList.size());
            jsonObject.put("list",adminLogs);

            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }
    @GetMapping("/admin/table/{key}")
    public ResponseMsg<JSONObject> find(int currentPage, int size,@PathVariable String key){
        try {
            List<AdminLog> productList = adminLogService.findAdminLogLike(key);
            int total = productList.size();
            JSONObject jsonObject = new JSONObject();
            List<AdminLog> categories;
            if(total<size*currentPage && total>(currentPage-1)*size){
                categories = productList.subList(size * (currentPage - 1), total);
            }else {
                categories = productList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",total);
            jsonObject.put("list",categories);
            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }

    @GetMapping("/account/index")
    public ResponseMsg<List<AccountLog>> getAccountLog(){
        return ResponseMsg.createForSuccess(accountLogService.getAccountLog());
    }
    @GetMapping("/account/table")
    public ResponseMsg<JSONObject> TableA(int currentPage, int size){
        try {
            List<AccountLog> accountLogList = accountLogService.getAccountLog();
            JSONObject jsonObject = new JSONObject();
            List<AccountLog> accountLogs;
            if(accountLogList.size()<size*currentPage && accountLogList.size()>(currentPage-1)*size){
                accountLogs = accountLogList.subList(size * (currentPage - 1), accountLogList.size());
            }else {
                accountLogs = accountLogList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",accountLogList.size());
            jsonObject.put("list",accountLogs);

            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }
    @GetMapping("/account/table/{key}")
    public ResponseMsg<JSONObject> findP(int currentPage, int size,@PathVariable String key){
        try {
            List<AccountLog> productList = accountLogService.findAccountLogLike(key);
            int total = productList.size();
            JSONObject jsonObject = new JSONObject();
            List<AccountLog> categories;
            if(total<size*currentPage && total>(currentPage-1)*size){
                categories = productList.subList(size * (currentPage - 1), total);
            }else {
                categories = productList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",total);
            jsonObject.put("list",categories);
            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }
}
